package components.scanario
{
	import UIFree.controls.uif_baseHint;
	import UIFree.controls.uif_close;
	import UIFree.controls.uif_hint;
	import UIFree.uif_component;
	import UIFree.uif_ecomponent;
	
	import components.c_image;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class c_assistantImage extends uif_ecomponent {
		private var _image	: c_image;
		private var _hint	: uif_baseHint;
		public function c_assistantImage(a_parent: uif_component, ax:int=0, ay:int=0, source: Object=null, a_className: String='') {
			super(a_parent, ax, ay, 0, 0, a_className);
			createComponents(source);
		}
		
		protected function createComponents(source: Object): void {
			buttonMode = true;
			_image	= new c_image(this);
			_hint 	= new uif_baseHint(this, 0, -100, 160, 90, 'hint');
			_hint.setVisibility(false);
			
			_image.addEventListener(Event.COMPLETE, doImageComplete);
			_image.source = source;
		}
		
		protected function doImageComplete(e: Event): void {
			var space: Number = getSpace(true);
			_image.removeEventListener(Event.COMPLETE, doImageComplete);
			_image.x = space;
			_image.y = space;
			resize(_image.width + space * 2, _image.height + space * 2);
			if (text) updateHintVisualisation();
			new uif_close(this, width - 10, -10);
		}
		
		public function get image(): c_image {
			return _image;
		}
		
		override protected function listenEvents():void {
			super.listenEvents();
			addEvent(MouseEvent.MOUSE_DOWN, doMouseDown);
			addEvent(Event.CLOSE, doClose);
		}
		
		private function doClose(e: Event): void {
			slideHide();
		}
		
		protected function doMouseDown(e: MouseEvent): void {
			beginDrag(2);
		}
		
		private function updateHintVisualisation(): void {
			_hint.slideShow();
			_hint.x = (_image.width - _hint.width) / 2;
			_hint.y = - _hint.height - 10;
		}
		
		public function get text(): String {
			return _hint.htmlText;
		}
		
		public function set text(a_text: String): void {
			if (a_text) {
				_hint.htmlText = a_text;
				_hint.height = _hint.textField.textHeight + _hint.getSpace(true) * 2;
				updateHintVisualisation();
			} else {
				_hint.slideHide(function(): void {
					_hint.text = '';
				});
			}
		}
	}
}